const {
    typesModel,
} = require("./models/typesModel")

const mongoose = require("mongoose")

module.exports.addTypes = async function (params) {
    const data = await typesModel.create(params)
    return data
}

// module.exports.getTypes = async function () {
//     const data = await typesModel.aggregate([{
//         $lookup: {
//             // 设置关联的集合名称
//             from: "insurances",
//             // 当前集合中做关联的属性名（字段）
//             localField: "_id",
//             // 被关联集合对应的属性名
//             foreignField: "typesId",
//             as: "insurancesData"
//         }
//     }])
//     return data
// }

module.exports.getAllTypes = async function () {
    return await typesModel.aggregate([{
        $lookup: {
            from: "insurances",
            localField: "_id",
            foreignField: "typesId",
            as: "insurancesData"
        }
    },
    {
        $match: {
            delete: false,
        }
    },
    ])
}

module.exports.isExist = async function (params) {
    const data = await typesModel.find(params)
    return data
}

module.exports.isNull = async function (params) {
    return await typesModel.aggregate([{
        $lookup: {
            from: "insurances",
            localField: "_id",
            foreignField: "typesId",
            as: "insurancesData"
        }
    },
    {
        $match: {
            delete: false,
            _id: mongoose.Types.ObjectId(params._id)
        }
    },
    ])
}

module.exports.delTypesById = async function (params) {
    return await typesModel.updateOne({
        _id: params
    }, {
        delete: true,
    })
}

module.exports.getTypesById = async function (params) {
    return await typesModel.find(params)
}

module.exports.changeTypesById = async function changeTypesById(params) {
    return await typesModel.updateOne({
        _id: params._id
    }, {
        ...params
    })
}